import $ from 'jquery';
import MotionFrame from './motion_frame'
import Mat3 from './mat3'
import ViewFactory from './view_factory'
import Color from './color'
import Vec2 from './vec2'

export default class MotionLayer {
  contentView = null
            updateFromXML (t) {
                let r = this,
                    i = t.find("> content > *");
                if (i.length > 0) {
                    let s = i[0].nodeName.toLowerCase();
                    r.contentView = ViewFactory.createViewByNodeName(s),
                        r.contentView != null && r.contentView.updatePropsFromXML(i)
                } else
                    r.contentView = null;
                r.frames = [];
                let o = t.find("> frame");
                o.each(function(t, n) {
                    let i = $(n),
                        s = new MotionFrame;
                    s.updateFromXML(i),
                        r.frames.push(s)
                })
            }
            calculateValue (e, t, n, r, i) {
                return e + (t - e) * i(n, 0, 1, r)
            }
            frames = null
            calculateRotation (e, n, r) {
                let s = new Vec2(e.width() / 2, e.height() / 2),
                    o = new Mat3;
                return o.a = Math.cos(r),
                    o.b = Math.sin(r),
                    o.c = -1 * o.b,
                    o.d = o.a,
                    o.tx = n.x - o.a * s.x - o.c * s.y,
                    o.ty = n.y - o.b * s.x - o.d * s.y,
                    o
            }
            contentWithTime (e) {
                let n = this,
                    s = null,
                    o = null;
                for (let u = n.frames.length - 1; u >= 0; u--) {
                    let a = n.frames[u];
                    if (a.time <= e) {
                        s = a,
                            u < n.frames.length - 1 && (o = n.frames[u + 1]);
                        break
                    }
                }
                let f = null;
                if (s != null && s.isBlank == 0) {
                    f = n.contentView;
                    if (s.tween == null || o == null)
                        f.alpha(s.alpha),
                        s.bgColor != null && f.bgColor(s.bgColor),
                        s.rotation != null ? f.matrix(n.calculateRotation(f, new Vec2(s.x, s.y), s.rotation)) : f.matrix(s.matrix),
                        f.goToAndPlay != undefined && isNaN(s.frame) == 0 && (o != null && isNaN(o.frame) == 0 ? f.goToAndStop(s.frame) : f.goToAndPlay(s.frame));
                    else {
                        let l = o.time - s.time,
                            c = e - s.time;
                        if (s.bgColor != null && o.bgColor != null) {
                            let h = new Color;
                            h.r(n.calculateValue(s.bgColor.r(), o.bgColor.r(), c, l, s.tween)),
                                h.g(n.calculateValue(s.bgColor.g(), o.bgColor.g(), c, l, s.tween)),
                                h.b(n.calculateValue(s.bgColor.b(), o.bgColor.b(), c, l, s.tween)),
                                h.a(n.calculateValue(s.bgColor.a(), o.bgColor.a(), c, l, s.tween)),
                                f.bgColor(h)
                        }
                        let p = n.calculateValue(s.alpha, o.alpha, c, l, s.tween);
                        f.alpha(p);
                        if (f.goToAndStop != undefined && isNaN(s.frame) == 0 && isNaN(o.frame) == 0) {
                            let d = Math.floor(n.calculateValue(s.frame, o.frame, c, l, s.tween));
                            f.goToAndStop(d)
                        }
                        if (s.rotation != null && o.rotation != null) {
                            let v = n.calculateValue(s.rotation.rotation, o.rotation.rotation, c, l, s.tween);
                            f.matrix(n.calculateRotation(f, new Vec2(s.rotation.x, s.rotation.y), v))
                        } else {
                            let m = s.matrix,
                                g = o.matrix,
                                y = new Mat3;
                            y.a = n.calculateValue(m.a, g.a, c, l, s.tween),
                                y.b = n.calculateValue(m.b, g.b, c, l, s.tween),
                                y.c = n.calculateValue(m.c, g.c, c, l, s.tween),
                                y.d = n.calculateValue(m.d, g.d, c, l, s.tween),
                                y.tx = n.calculateValue(m.tx, g.tx, c, l, s.tween),
                                y.ty = n.calculateValue(m.ty, g.ty, c, l, s.tween),
                                f.matrix(y)
                        }
                    }
                }
                return f
            }
            reverse () {
                let e = this,
                    t = e.frames.slice(),
                    n = [];
                for (let r = 0; r < t.length; r++) {
                    let i = t[r];
                    n.push(i.time)
                }
                t = t.reverse();
                for (let s = 0; s < t.length; s++) {
                    let o = t[s];
                    o.time = n[s]
                }
                e.frames = t
            }
            clone () {
                let e = this,
                    t = new MotionLayer;
                e.contentView != null && (t.contentView = e.contentView.clone()),
                    t.frames = [];
                for (let n = 0; n < e.frames.length; n++) {
                    let r = e.frames[n];
                    t.frames.push(r)
                }
                return t
            }
            dealloc () {
                let e = this;
                e.contentView.dealloc(),
                    e.contentView = null;
                for (let t = 0; t < e.frames.length; t++)
                    e.frames[t].dealloc();
                e.frames = null
            }
}